﻿{include file="base/_meta" /}
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">标题:</label>
                        <div class="layui-input-inline">
                            <input name="title" class="layui-input" placeholder="标题"/>
                        </div>
                    </div>
                    <div class="layui-inline layui-btn-group">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="userTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button class="layui-btn icon-btn" onclick="window.location.reload();">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>&nbsp;
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="restore">恢复</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="TbShow">
    <input type="checkbox" lay-filter="TbShowCk" value="{{d.id}}" lay-skin="switch"
           lay-text="显示|隐藏" {{d.show==10?'checked':''}} style="display: none;" readonly disabled/>
    <p style="display: none;">{{d.show==10?'显示':'隐藏'}}</p>
</script>

<script type="text/html" id="TbTop">
    <input type="checkbox" lay-filter="TbTopCk" value="{{d.id}}" lay-skin="switch"
           lay-text="置顶|不置顶" {{d.top==10?'checked':''}} style="display: none;" readonly disabled/>
    <p style="display: none;">{{d.top==10?'置顶':'不置顶'}}</p>
</script>


<!-- js部分 -->
{include file="base/_script" /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect','index'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var index = layui.index;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#userTable',
            url: "{:url('article/recycle')}",
            page: true,
            toolbar: ['<p>',
                '<button lay-event="restore" class="layui-btn layui-btn-sm icon-btn">恢复</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'id', title: 'ID'},
                {field: 'title', title: '名称'},
                {field: 'sort', title: '排序', sort: true},
                {field: 'state', title: '状态', templet: '#TbShow', sort: true, width: 100},
                {field: 'state', title: '置顶', templet: '#TbTop', sort: true, width: 100},
                {title: '操作', toolbar: '#userTbBar', align: 'center', minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(userTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(userTable)', function (obj) {
            if (obj.event === 'restore') { // 修改
                doRestore(obj)
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(userTable)', function (obj) {
            if (obj.event === 'restore') { // 添加
                var checkRows = table.checkStatus('userTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doRestore({ids: ids});
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('userTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.get("{:url('article/article_del_true')}", {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
        function doRestore(obj) {
            layer.confirm('确定要恢复选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.get("{:url('article/article_restore')}", {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
    });
</script>
</body>
</html>
